package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Category;
import entity.Country;
import entity.Movie;
import entity.MoviePage;
import mapper.CategoryMapper;
import mapper.CountryMapper;
import mapper.MovieMapper;
import mapper.RelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 郝雷蒙
 * @Description
 * @date 2020/12/17 19:51
 */
@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private RelationMapper relationMapper;
    @Autowired
    private CountryMapper countryMapper;


    @Override
    public MoviePage getAllMovies(List<Integer> categoryIds,Integer pageNum, Integer pageSize) {
        //设置页码和页面大小
        PageHelper.startPage(pageNum,pageSize);
        //查询，查询语句被pagehelper拦截
        List<Movie> movies = movieMapper.selectAllMovies(categoryIds);
        //分页
        PageInfo<Movie> info = new PageInfo<>(movies);
        //转换
        MoviePage moviePage = new MoviePage();
        moviePage.setMovieList(info.getList());
        moviePage.setPageCount(info.getPages());
        moviePage.setRowCount((int)info.getTotal());
        moviePage.setPageNum(pageNum);
        moviePage.setPageSize(pageSize);

        return moviePage;
    }

    @Override
    public List<Category> getAllCategories() {
        return categoryMapper.selectAllCategories();
    }

    @Override
    public Movie getMovieById(Integer id) {
        return movieMapper.selectMovieById(id);
    }

    @Transactional
    @Override
    public Integer addOneMovie(Movie movie, List<Integer> categories) {
        if (movieMapper.insertOneMovie(movie) ==0){
            return 0;
        }
        //插入分类中间表
        relationMapper.insertMovieCategories(movie.getId(),categories);

        return 1;
    }

    @Override
    public Integer setOneMovie(Movie movie, List<Integer> categories) {

        if (movieMapper.updateOneMovie(movie) ==0){
            return 0;
        }
        //将图书的id转换为list
        List<Integer> list = new ArrayList<>();
        list.add(movie.getId());
        //从中间表删除所有的分类
        relationMapper.deleteCategoriesByMovieList(list);

        //插入分类中间表
        relationMapper.insertMovieCategories(movie.getId(),categories);

        return 1;
    }

    @Transactional
    @Override
    public Integer delMovieByList(List<Integer> movieList) {
        //删除多个图书
        movieMapper.deleteMovieByList(movieList);

        //删除图书分类
        relationMapper.deleteCategoriesByMovieList(movieList);

        return 1;
    }

    @Override
    public List<Country> getAllCountries() {
        return countryMapper.selectAllCountries();
    }

    @Override
    public Category getCategoryById(Integer categoryId) {
        return categoryMapper.selectCategoryById(categoryId);
    }
}
